package mirabilis.org;

import java.util.ArrayList;
import java.util.ListIterator;

import random.CommonState;

import mirabilis.org.config.Configuration;
import mirabilis.org.simplices.Coordinates;
import mirabilis.org.simplices.Vertex;
import mirabilis.samples.cec2014.Cec2014;



public abstract class TwoDimensions extends Thread{
	
	public static void main(String[] args) {
		Configuration.F=2;
		Configuration.N=2;
		Configuration.upper_bound= 100;
		Configuration.lower_bound= -100;	
		Cec2014 bench = new Cec2014();
		
		
		double scaleratio = (Configuration.upper_bound - Configuration.lower_bound) - Math.abs(Configuration.lower_bound);
		double radius = Math.sqrt(((Configuration.upper_bound - Configuration.lower_bound)*(Configuration.upper_bound - Configuration.lower_bound)/2.0));
		
		double originx = 0;//CommonState.r.nextDouble()*scaleratio;
		double originy = 0;//CommonState.r.nextDouble()*scaleratio;
		
		double step = 20;//radius;
		double cum = 0;
		int dirx = 1;//CommonState.r.nextBoolean()?-1:1;
		int diry = 1;//CommonState.r.nextBoolean()?-1:1;
		
				
		double[] genes = {originx,originy};
		double fo = bench.evaluate(genes); 
		//System.out.println(origin+" "+fo);
		
		double planetprimax = Double.MAX_VALUE;
		double planetprimay = Double.MAX_VALUE;
		
		for(int i=0;i<100;i++){
			
			double planetx,planety;
			if((dirx*step) +originx < Configuration.lower_bound){
				planetx = Configuration.upper_bound - Math.abs(((dirx*step) +originx)%Configuration.lower_bound);
			}else if ((dirx*step) +originx > Configuration.upper_bound){
				planetx = Configuration.lower_bound + ((dirx*step) +originx)%Configuration.upper_bound;
			}else
				planetx = (dirx*step) +originx;
			
			//System.out.println(" Planet x "+planetx);
			
			if((diry*step) +originy < Configuration.lower_bound){
				planety = Configuration.upper_bound - Math.abs(((diry*step) +originy)%Configuration.lower_bound);
			}else if ((diry*step) +originy > Configuration.upper_bound){
				planety = Configuration.lower_bound + ((diry*step) +originy)%Configuration.upper_bound;
			}else
				planety = (diry*step) +originy;
			
			
			double[] newplanet = {planetx,planety};
			double fplanet = bench.evaluate(newplanet);
			
			//if (planetprimay < Double.MAX_VALUE)
			//	System.out.println(planetprimax+" "+planetprimay+" "+planetx+","+planety+" "+dirx+","+diry+" "+fplanet+" "+fo+" "+step);
			//else
				System.out.println(originx+" "+originy+" "+planetx+" "+planety+" "+dirx+","+diry+" "+fplanet+" "+fo+" "+step);
			
				
				
			if (fplanet <= fo){
				originx = planetx;
				originy = planety;
				fo = fplanet;
				
				dirx = CommonState.r.nextBoolean()?-1:1;
				diry = CommonState.r.nextBoolean()?-1:1;
				
				//cum = step;
				//step = Math.sqrt((step*step)/2.0);
				//step = Math.sqrt(((cum+step)*(cum+step))/2.0);
				//step = Math.sqrt((step*step)*0.840896415);
				cum = 0;
				
			}else{
				//int auxx = changeXgraydirection(dirx, diry);
				//int auxy = changeYgraydirection(dirx, diry);
				//dirx = auxx;
				//diry = auxy;
				//step = Math.sqrt(((cum+step)*(cum+step))/2.0);
				//cum = 0;
				cum = step;
				//step = Math.sqrt((step*step)/2.0);
				step = Math.sqrt((step*step)*0.840896415);
			}
			//System.out.println(planet+" "+fplanet);
			
			int auxx = changeXgraydirection(dirx, diry);
			int auxy = changeYgraydirection(dirx, diry);
			dirx = auxx;
			diry = auxy;
			
			planetprimax = planetx;
			planetprimay = planety;
		}
		
	}
	// xy cycle
	// 00
	// 01
	// 11
	// 10
	public static int changeXgraydirection(int x, int y){
		if (x==-1 && y==-1){
			return -1;
		}else if(x==-1 && y==1){
			return 1;
		}else if(x==1 && y==1){
			return 1;
		}else {
			return -1;
		} 

	}
	// xy cycle
		// 00
		// 01
		// 11
		// 10
	public static int changeYgraydirection(int x, int y){
		if (x==-1 && y==-1){
			return 1;
		}else if(x==-1 && y==1){
			return 1;
		}else if(x==1 && y==1){
			return -1;
		}else {
			return -1;
		} 

	}
}
